package com.centling.smartSealForPhone.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.TextView;

import com.centling.smartSealForPhone.R;

/**
 *
 * @author fionera
 * @date 16-9-26
 */
public class AlignTextView
        extends TextView {

    private static final int SUFFIX_MODE_FULL_WIDTH = 1;
    private static final int SUFFIX_MODE_HALF_WIDTH = 2;
    private String text;
    private int suffixMode;

    private Paint paint;
    private Rect targetRect;

    public AlignTextView(Context context) {
        this(context, null);
    }

    public AlignTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AlignTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.AlignTextView);
        text = typedArray.getString(R.styleable.AlignTextView_text);
        suffixMode = typedArray.getInt(R.styleable.AlignTextView_suffixMode, 0);
        typedArray.recycle();

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setTextSize(getTextSize());
        paint.setColor(getTextColors().getDefaultColor());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (targetRect == null) {
            targetRect = new Rect(0, 0, getWidth(), getHeight());
        }

        Paint.FontMetricsInt fontMetrics = paint.getFontMetricsInt();
        int baseline = (targetRect.bottom + targetRect.top - fontMetrics.bottom - fontMetrics
                .top) / 2;
        float textPx = getTextSize();
        int maxTextViewWidth = getWidth();

        if (TextUtils.isEmpty(text)) {
            super.onDraw(canvas);
            return;
        }

        if (suffixMode == 0) {
            char[] chars = text.toCharArray();
            int spec = (int) ((maxTextViewWidth - chars.length * textPx) / (chars.length - 1));
            for (int i = 0; i < chars.length; i++) {
                canvas.drawText(String.valueOf(chars[i]), i * (textPx + spec), baseline, paint);
            }
        } else {
            String suffixStr = text.substring(text.length() - 1, text.length());
            int suffixWidth = getSuffixWidth(paint, suffixStr, suffixMode);
            char[] chars = text.substring(0, text.length() - 1).toCharArray();
            int spec = (int) ((maxTextViewWidth - chars.length * textPx - suffixWidth) / (chars
                    .length - 1));
            for (int i = 0; i < chars.length; i++) {
                canvas.drawText(String.valueOf(chars[i]), i * (textPx + spec), baseline, paint);
            }
            canvas.drawText(suffixStr, maxTextViewWidth - suffixWidth, baseline, paint);
        }
        super.onDraw(canvas);
    }

    private int getSuffixWidth(Paint paint, String suffixStr, int suffixMode) {
        if (TextUtils.isEmpty(suffixStr) || suffixMode == 0) {
            return 0;
        }
        Rect rect = new Rect();
        switch (suffixMode) {
            case SUFFIX_MODE_FULL_WIDTH:
                paint.getTextBounds("啊", 0, 1, rect);
                break;
            case SUFFIX_MODE_HALF_WIDTH:
                paint.getTextBounds("a", 0, 1, rect);
                break;
            default:
                break;
        }
        return rect.width();
    }

    public void setAlignText(String text) {
        this.text = text;
        invalidate();
    }
}
